package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import bean.Masterbean;
import bean.Petbean;
import bean.Userbean;
import bean.Vetbean;
import conn.ConnectDatabase;
import service.IVetDao;

public class VetDaoImp implements IVetDao {
	
	 PreparedStatement ps = null;
		Date date = new Date();
		//得到一个timestamp格式的时间，存入mysql中的时间格式为"yyyy-MM-dd HH:mm:ss"
		Timestamp timestamp = new Timestamp(date.getTime());
	@Override
	public List<Vetbean> queryVet(Map<String, Object> map) {
		StringBuffer sql= new StringBuffer("select * from vet where 1 =1");
		List<Vetbean> list = new ArrayList<Vetbean>();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			Vetbean p = new Vetbean();
			p.setId(rs.getInt("id"));
			p.setName(rs.getString("name"));
			p.setSubject(rs.getString("subject"));
			p.setTel(rs.getInt("tel"));
			list.add(p);	
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return list;
		
	}

	@Override
	public Vetbean queryById(Vetbean p) {
		Vetbean pet = null;
		String sql = "select * from vet where id="+p.getId();
		
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			pet  = new Vetbean();
			pet.setId(rs.getInt("id"));
			pet.setName(rs.getString("name"));
			pet.setSubject(rs.getString("subject"));
			pet.setTel(rs.getInt("tel"));
					
					
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pet;
	}

	@Override
	public boolean delete(Vetbean p) {
		String sql ="delete from vet where id="+p.getId();
		String sql1= "insert into history(description,data) values(?,?)";
		int n=0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		n=ps.executeUpdate();
		
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "删除了一次兽医信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return n>0;
	}

}
